package com.tangyee.home_video;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tangyee.R;
import com.tangyee.home_blood_center.activities.MainActivity;
import com.tangyee.refreshloadlistview.SimpleFooter;
import com.tangyee.refreshloadlistview.SimpleHeader;
import com.tangyee.refreshloadlistview.ZrcListView;

import org.jsoup.select.Evaluator;

import java.util.ArrayList;
import java.util.HashMap;

public class HealthyVideo extends AppCompatActivity implements View.OnClickListener{
    private ZrcListView videoListView;
    private Handler handler;
    //返回键
    private ImageView videotohome;
    private int Id = 1;

    private VideoAdapter videoadapter;
    private ArrayList<HashMap<String, Object>> videoListItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_home_video);
        videoListView=(ZrcListView)findViewById(R.id.videoListView);
        videotohome=(ImageView)findViewById(R.id.videoTohome);
        videotohome.setOnClickListener(this);
        videoListItem=new ArrayList<HashMap<String, Object>>();
        handler = new Handler();

        // 设置下拉刷新的样式（可选，但如果没有Header则无法下拉刷新）
        SimpleHeader header = new SimpleHeader(this);
        header.setTextColor(0xff159813);
        header.setCircleColor(0xff159813);
        videoListView.setHeadable(header);

        // 设置加载更多的样式（可选）
        SimpleFooter footer = new SimpleFooter(this);
        footer.setCircleColor(0xff159813);
        videoListView.setFootable(footer);

        // 设置列表项出现动画（可选）
        videoListView.setItemAnimForTopIn(R.anim.topitem_in);
        videoListView.setItemAnimForBottomIn(R.anim.bottomitem_in);

        // 下拉刷新事件回调（可选）
        videoListView.setOnRefreshStartListener(new ZrcListView.OnStartListener() {
            @Override
            public void onStart() {
                refresh();
            }
        });
        // 加载更多事件回调（可选）
        videoListView.setOnLoadMoreStartListener(new ZrcListView.OnStartListener() {
            @Override
            public void onStart() {
                loadMore();
            }
        });
        //当点击每条目
        videoListView.setOnItemClickListener(new ZrcListView.OnItemClickListener() {
            @Override
            public void onItemClick(ZrcListView parent, View view, int position, long id) {
                //Toast.makeText(HealthyVideo.this,videoListItem.get(position).get("videotitle").toString(),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("type",videoListItem.get(position).get("videotype").toString());
                intent.putExtra("desc",videoListItem.get(position).get("videodesc").toString());
                intent.putExtra("title",videoListItem.get(position).get("videotitle").toString());
                intent.setClass(HealthyVideo.this, HealthyVideoDetails.class);
                startActivity(intent);
            }
        });
        videoadapter = new VideoAdapter(HealthyVideo.this,videoListItem);
        videoListView.setAdapter(videoadapter);
        videoListView.refresh(); // 主动下拉刷新

    }
    private void refresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                HashMap<String, Object> map;
                for(int i=0;i<8;i++){
                    map=new HashMap<String, Object>();
                    map.put("videoimg",R.drawable.video);
                    map.put("videotitle", "为什么2型糖尿病可以逆转？");
                    map.put("videotype","视频类型：逆转2型糖尿病系列");
                    map.put("videodesc","视频说明：你知道吗？有相当一部分2型糖尿病可以逆转，特别是糖尿病病程短、肥胖、控制血糖意愿强烈、依从性高的患者。你的糖尿病能逆转吗？浙江大学医学院附属第二医院内分泌科单鹏飞主任医师和精神科禹华良副主任医师专家为您讲解为什么2型糖尿病可以逆转。");
                    videoListItem.add(map);
                }

                videoadapter.notifyDataSetChanged();
                videoListView.setRefreshSuccess("加载成功"); // 通知加载成功
                videoListView.startLoadMore(); // 开启LoadingMore功能

                //videoListView.setRefreshFail("加载失败");
            }
        }, 1 * 1000);
    }

    private void loadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                HashMap<String, Object> map;
                for(int i=0;i<8;i++){
                    map=new HashMap<String, Object>();
                    map.put("videoimg",R.drawable.video);
                    map.put("videotitle", "为什么2型糖尿病可以逆转？");
                    map.put("videotype","视频类型:逆转2型糖尿病系列");
                    map.put("videodesc","视频说明：你知道吗？有相当一部分2型糖尿病可以逆转，特别是糖尿病病程短、肥胖、控制血糖意愿强烈、依从性高的患者。你的糖尿病能逆转吗？浙江大学医学院附属第二医院内分泌科单鹏飞主任医师和精神科禹华良副主任医师专家为您讲解为什么2型糖尿病可以逆转。");
                    videoListItem.add(map);
                    Id++;
                }

                videoadapter.notifyDataSetChanged();
                videoListView.setLoadMoreSuccess();

                //videoListView.stopLoadMore();
            }
        }, 1 * 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.videoTohome:
                finish();
                break;
        }
    }

    private class VideoAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        //得到一个LayoutInfalter对象用来导入布局
        private ArrayList<HashMap<String, Object>> videoListItem;
        public VideoAdapter(Context context, ArrayList<HashMap<String, Object>> listItem) {
            this.inflater =LayoutInflater.from(context);
            this.videoListItem = listItem;
        }
        @Override
        public int getCount() {
            return videoListItem==null?0:videoListItem.size();
        }

        @Override
        public Object getItem(int position) {
            return videoListItem.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        class VideoViewHolder
        {
            public ImageView img;
            public TextView title;
            public TextView type;
            public TextView desc;
        }//声明一个外部静态类

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final VideoViewHolder holder ;
            if (convertView == null) {
                holder = new VideoViewHolder();
                convertView = inflater.inflate(R.layout.layout_home_videoitem, null);
                holder.img = (ImageView)convertView.findViewById(R.id.video_item);
                holder.title = (TextView)convertView.findViewById(R.id.video_title);
                holder.type=(TextView)convertView.findViewById(R.id.video_type) ;
                holder.desc=(TextView)convertView.findViewById(R.id.video_desc);
                convertView.setTag(holder);
            }else {
                holder = (VideoViewHolder)convertView.getTag();
            }
            holder.img.setImageResource((Integer) videoListItem.get(position).get("videoimg"));
            holder.title.setText((String) videoListItem.get(position).get("videotitle"));
            holder.type.setText((String)videoListItem.get(position).get("videotype"));
            holder.desc.setText((String)videoListItem.get(position).get("videodesc"));
            return convertView;
        }
    }
}
